package org.dtrd.modules.dict.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dtrd.modules.dict.entity.bean.BankInfoDetail;
import org.dtrd.modules.dict.entity.bean.BankInfoListInfo;
import org.dtrd.modules.dict.entity.po.DtrdEntRdDicBank;
import org.dtrd.modules.dict.entity.request.BankInfoListRequest;
import org.dtrd.modules.dict.mapper.DtrdEntRdDicBankMapper;
import org.dtrd.modules.dict.service.IDtrdEntRdDicBankService;
import org.dtrd.common.util.StringUtil;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author QR
 * @since 2021-12-13
 */
@DS("multi-datasource1")
@Service
public class DtrdEntRdDicBankServiceImpl extends ServiceImpl<DtrdEntRdDicBankMapper, DtrdEntRdDicBank> implements IDtrdEntRdDicBankService {

    @Override
    public List<BankInfoListInfo> getBankInfoList(BankInfoListRequest request) {
        LambdaQueryWrapper<DtrdEntRdDicBank> queryWrapper = baseQueryWrapper();
        String bankName = request.getBankName();
        if (StrUtil.isNotBlank(bankName)) {
            queryWrapper.like(DtrdEntRdDicBank::getBankName, StringUtil.fuzzySearchWrapper(bankName));
        }
        List<DtrdEntRdDicBank> poList = list(queryWrapper);
        List<BankInfoListInfo> dtoList = new ArrayList<>(poList.size());
        if (!poList.isEmpty()) {
            dtoList = poList.stream().map(BankInfoListInfo::new).collect(Collectors.toList());
        }
        return dtoList;
    }

    @Override
    public BankInfoDetail getBankInfo(Integer dataId) {
        DtrdEntRdDicBank dtrdEntRdDicBank = getById(dataId);
        return Optional.ofNullable(dtrdEntRdDicBank)
                        .map(po -> new BankInfoDetail().parseFromPo(po))
                        .orElse(null);
    }

    @Override
    public boolean saveOrUpdateBankInfo(BankInfoDetail dto) {
        DtrdEntRdDicBank dtrdEntRdDicBank = new DtrdEntRdDicBank();
        if(dto.getDataId() == null){
            dtrdEntRdDicBank = dtrdEntRdDicBank.init();
        }
        dtrdEntRdDicBank.parseFromDto(dto);
        return saveOrUpdate(dtrdEntRdDicBank);
    }

    @Override
    public boolean removeBankInfo(Integer dataId) {
        return removeById(dataId);
    }

    private LambdaQueryWrapper<DtrdEntRdDicBank> baseQueryWrapper() {
        return Wrappers.lambdaQuery(DtrdEntRdDicBank.class)
                        .eq(DtrdEntRdDicBank::getIsDel, 0);
    }

    private LambdaUpdateWrapper<DtrdEntRdDicBank> baseUpdateWrapper() {
        return Wrappers.lambdaUpdate(DtrdEntRdDicBank.class)
                        .eq(DtrdEntRdDicBank::getIsDel, 0);
    }
}
